package com.eunion.web.common.controller;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.eunion.web.common.verifycode.Captcha;
import com.eunion.web.common.verifycode.SpecCaptcha;

/**
 * 首页的跳转 From:https://springframework.guru/spring-boot-web-application-part-2-using -thymeleaf/
 * 
 * @version 2015-8-2
 */
@Controller
// @RequestMapping(value = "/")
public class MainController {

	@Autowired
	protected HttpSession httpSession;

	private Logger LOG = LoggerFactory.getLogger(this.getClass());

	/**
	 * @return 跳转管理员页面.
	 */
	@RequestMapping("/")
	public String index() {
		// return "/cmp";
		// return "forward:/hello" => 转发到能够匹配 /hello 的 controller 上
		// return "hello" => 实际上还是转发，只不过是框架会找到该逻辑视图名对应的 View 并渲染
		// return "/hello" => 同 return "hello"
		return "forward:/admin/";
		// 重定向到 /hello 资源
		// return "redirect:/hello";
	}

	// @RequestMapping(value = "/lang/{lang}", method = {RequestMethod.GET})
	// public void lang(@PathVariable String lang) {
	// if (lang.equals("zh")) {
	// Locale locale = new Locale("zh", "CN");
	// httpSession.setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, locale);
	// } else if (lang.equals("en")) {
	// Locale locale = new Locale("en", "US");
	// httpSession.setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, locale);
	// } else {
	// httpSession.setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, LocaleContextHolder.getLocale());
	// }
	// return;
	// }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) HttpServletRequest request
	 */
	@RequestMapping(value = "/verifyCode", method = {RequestMethod.GET})
	public void verifyCode(HttpServletRequest request, HttpServletResponse response) {
		try {

			/**
			 * jgp格式验证码 宽，高，位数。
			 */
			Captcha captcha = new SpecCaptcha(100, 35, 4);
			captcha.out(request, response, "_vcode");// 输出

			// String code = VerifyCodeUtil.sendVarifyCode(request, response);
			// LOG.debug("created verify code is {}", code);
		} catch (Exception e) {
			LOG.error("获取验证码异常", e);
		}
	}

}
